<?php

namespace App\Http\Controllers\CRM\MiniAPP;

use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\MiniAPP\MemberRechargeOptionRequest;
use App\Http\Resources\MiniAPPMemberRechargeOptionResource;
use App\Models\MiniappMemberRechargeOption;
use Illuminate\Http\Request;

class MemberRechargeOptionController extends Controller
{
    public function getList()
    {
        $options = MiniappMemberRechargeOption::latest()->get();

        return MiniAPPMemberRechargeOptionResource::collection($options);
    }

    public function getInfo(MiniappMemberRechargeOption $option)
    {
        return new MiniAPPMemberRechargeOptionResource($option);
    }

    public function create(MemberRechargeOptionRequest $request)
    {
        if ($request->is_gift) {
            $option = MiniappMemberRechargeOption::create($request->only([
                'money', 'is_gift', 'gift_name',
            ]));
        } else {
            $option = MiniappMemberRechargeOption::create($request->only([
                'money', 'is_gift', 'give_table_money',
            ]));
        }

        return new MiniAPPMemberRechargeOptionResource($option);
    }

    public function edit(MiniappMemberRechargeOption $option, MemberRechargeOptionRequest $request)
    {
        if ($request->is_gift) {
            $option->update($request->only([
                'money', 'is_gift', 'gift_name',
            ]) + ['give_table_money' => 0]);
        } else {
            $option->update($request->only([
                'money', 'is_gift', 'give_table_money',
            ]) + ['gift_name' => null]);
        }

        return new MiniAPPMemberRechargeOptionResource($option);
    }

    public function remove(MiniappMemberRechargeOption $option)
    {
        $option->delete();

        return ApiResponseHelper::json([]);
    }
}
